<?php

namespace app\controllers\h5;

use app\controllers\h5\base\H5Controller;
use app\models\News;
use app\models\product\Product;
use app\models\product\ProductArea;
use app\models\product\ProductAreaList;
use app\models\product\ProductCategory;

class GoHomeController extends H5Controller
{
    public function actionProduct()
    {
        // tmd category
        $list = ProductCategory::find()
            ->where(['!=', 'status', ProductCategory::$statusMapping['DELETE']])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
            ->with(['image'])
            ->asArray()
            ->all();
        return $this->successEncode(['list' => $list]);

        $ids = ProductAreaList::find()
            // ->where(['area_id' => ProductArea::$KEY['REPORT']])
            ->where(['area_id' => ProductArea::$KEY['GENERAL']])
            ->orWhere(['area_id' => ProductArea::$KEY['REPORT']])
            ->orWhere(['area_id' => ProductArea::$KEY['REPEAT']])
            ->select(['product_id'])
            ->column();

        // var_dump($ids);exit;

        $list = Product::find()
            ->where(['id' => $ids])
            ->andWhere(['is_recommend' => 0])
            ->andWhere(['status' => Product::$statusMapping['OK']])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->with(['mainImage'])
            ->asArray()
            ->all();

        return $this->successEncode(['list' => $list]);
    }

    public function actionNews()
    {
        $query = News::find()
            ->where(['status' => News::$statusMapping['OK']])
            ->andWhere(['is_home' => 1])
            ->andWhere(['>', 'image_id', 0]);

        $list = $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->with(['image'])
            ->asArray()
            ->all();

        return $this->successEncode(['list' => $list]);
    }
}